package com.itlong.cloud.smartcard.service.impl;

import com.itlong.cloud.POJO.PO.PushInfoPO;
import com.itlong.cloud.POJO.PO.PushInfoProjectPO;
import com.itlong.cloud.POJO.VO.smartcard.SmartCardOldAdVO;
import com.itlong.cloud.POJO.VO.smartcard.SmartCardOldPushVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.operate.dao.IOperatePushInfoDao;
import com.itlong.cloud.operate.dao.IOperatePushInfoProjectDao;
import com.itlong.cloud.smartcard.dao.ISmartCardOldPushDao;
import com.itlong.cloud.smartcard.service.ISmartCardOldPushService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.Collator;
import java.util.*;

/**
 * <desc>
 *      旧一卡通_推送服务接口实现。
 * <desc/>
 *
 * @createDate 2017-12-13.
 */
@Service
public class SmartCardOldPushServiceImpl implements ISmartCardOldPushService{

    //一卡通推送消息数据操作接口
    @Autowired
    ISmartCardOldPushDao iSmartCardOldPushDao;

    //通知公告数据操作接口
    @Autowired
    IOperatePushInfoDao iOperatePushInfoDao;

    //消息推送对应项目数据访问接口
    @Autowired
    IOperatePushInfoProjectDao iOperatePushInfoProjectDao;
    /**
     * <desc>
     *      获取推送列表。
     * <desc/>
     *
     * @param  projectId 项目编号
     * @param  pageNo 当前页
     * @param  pageSize 每页显示的记录数
     * @return
     * @author shengen.T
     * @createDate 2017-12-13
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public Page<SmartCardOldPushVO> getPushInfoList(String projectId,String pageNo,String pageSize) throws Exception{
        //分页类默认pageSize为20
        Page<SmartCardOldPushVO> page = new Page<>();
        List<SmartCardOldPushVO> pushListVO  = null;
        Integer pageCount=0;
        //获取推送消息列表
        List<SmartCardOldPushVO>  pushInfoList = iSmartCardOldPushDao.getPushInfoList(projectId);
        //获取推送广告列表
        List<SmartCardOldAdVO> adList = iSmartCardOldPushDao.getAdList(projectId);
        //根据最新的推送消息和广告比对最新的10条记录，返回给一卡通
        if((pushInfoList!=null && pushInfoList.size()>0) && (adList!=null &&adList.size()>0)){
            pushListVO =getPushListVO(pushInfoList, getAdist(adList));
        }else if((adList!=null &&adList.size()>0) && (pushInfoList==null)){
            pushListVO = getAdist(adList);
        }else if((adList==null)&&(pushInfoList!=null && pushInfoList.size()>0)){
            pushListVO = pushInfoList;
        }
        //获取行数
        if(pushListVO!=null && pushListVO.size()>0){
            pageCount = pushListVO.size();
        }
        //填充返回数据集
        page.setRows(pushListVO,pageCount);
        return page;
    }

     /**
      * <desc>
      *      获取广告列表。
      * <desc/>
      *
      * @param adList 广告列表
      * @return
      * @author shengen.T
      * @createDate 2017-12-14
      */
    private List<SmartCardOldPushVO> getAdist(List<SmartCardOldAdVO> adList) throws Exception{
        List<SmartCardOldPushVO> AdVOList = new ArrayList<>();
        Integer count=0;
        if((adList!=null) && (adList.size()>0)){
            for (SmartCardOldAdVO adVO:adList){
                List<SmartCardOldPushVO> adInfoList = iSmartCardOldPushDao.getPushAdList(adVO.getAdId());
                SmartCardOldPushVO pushAdVo = new SmartCardOldPushVO();
                if(adInfoList!=null && adInfoList.size()>0){
                    for (SmartCardOldPushVO adImg:adInfoList){
                        pushAdVo.setInfoType("2");
                        pushAdVo.setImgUrl(adImg.getImgUrl());
                        pushAdVo.setInfoContent(adImg.getInfoContent());
                        pushAdVo.setCreateTime(adVO.getCreateTime());
                        pushAdVo.setStartTime(adVO.getStartTime());
                        pushAdVo.setEndTime(adVO.getEndTime());
                        AdVOList.add(pushAdVo);
                        count++;
                        //只取10条记录
                        if(count==10){
                            return AdVOList;
                        }
                    }
                }
            }
        }

        return AdVOList;
    }

     /**
      * <desc>
      *      获取推送列表转换。
      * <desc/>
      *
      * @param  pushInfoList 通知公告
      * @param  pushAdList  广告
      * @return
      * @author shengen.T
      * @createDate 2017-12-14
      */
    private List<SmartCardOldPushVO> getPushListVO(List<SmartCardOldPushVO> pushInfoList,List<SmartCardOldPushVO> pushAdList) throws Exception{
        List<SmartCardOldPushVO> AdVOList = new ArrayList<>();
        AdVOList.addAll(pushInfoList);
        AdVOList.addAll(pushAdList);
        Collections.sort(AdVOList,(o1, o2) -> {
            Collator collator = Collator.getInstance(Locale.CHINA);
            return collator.compare(o1.getCreateTime(), o2.getCreateTime());
        });
        Collections.reverse(AdVOList);
        return AdVOList.subList(0,10);
    }


    /**
     * <desc>
     *      增加推送。
     * <desc/>
     *
     * @param projectId 项目编号
     * @param infoContent 消息内容
     * @param startTime 开始推送时间
     * @param endTime  结束推送时间
     * @return
     * @author shengen.T
     * @createDate 2017-12-18
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean addpushInfo(String projectId,String infoContent,String startTime,String endTime) throws Exception{
        boolean flag =false;
        //保存消息推送内容
        PushInfoPO pushInfoPO = new PushInfoPO(
                LogicIdUtil.bussinessId(),
                2,
                infoContent,
                1,
                startTime,
                endTime,
                new Date(),
                new Date()
        );
        Integer pushInfoCount =0;
        pushInfoCount = iOperatePushInfoDao.save(SqlUtil.durableData(pushInfoPO, PlatformConstants.TABLE_SAVE));

        //保存推送消息和项目对应信息
        PushInfoProjectPO pushInfoProjectPO = new PushInfoProjectPO();
        Integer pushProjectCount =0;
        pushInfoProjectPO.setProjectId(projectId);
        pushInfoProjectPO.setPushInfoId(pushInfoPO.getPushInfoId());
        pushProjectCount = iOperatePushInfoProjectDao.save(SqlUtil.durableData(pushInfoProjectPO, PlatformConstants.TABLE_SAVE));
        if((pushInfoCount==1)&&(pushProjectCount==1)){
            flag=true;
        }
        return flag;
    }
}
